package com.baibei.ebec.follow.adapter;

import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.ebec.follow.fragment.LastOrdersFragment;
import com.baibei.ebec.home.R;
import com.blankj.utilcode.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by tang on 2018/8/16.
 */

public class LastOrdersAdapter extends RecyclerView.Adapter<LastOrdersAdapter.ViewHolder>{
    private JSONArray datas;

    public LastOrdersAdapter(JSONArray datas) {
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_last_orders, null);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        JSONObject json = datas.optJSONObject(i);
        if ("3".equals(json.optString("isAuto"))){
            viewHolder.ivHandOrAuto.setImageResource(R.drawable.img_instore);
            viewHolder.tvProfitContent.setText("---");
            viewHolder.tvSellPrice.setText("平仓价  ---");
            viewHolder.tvSellTime.setText("平仓时间  ---");
        } else {
            if ("0".equals(json.optString("isAuto"))){
                viewHolder.ivHandOrAuto.setImageResource(R.drawable.img_auto);
            } else {
                viewHolder.ivHandOrAuto.setImageResource(R.drawable.img_hand);
            }
            viewHolder.tvProfitContent.setText(Double.parseDouble(json.optString("plamount"))>=0
                    ?"+"+json.optString("plamount")+"元"
                    :json.optString("plamount")+"元");
            viewHolder.tvProfitContent.setTextColor(Double.parseDouble(json.optString("plamount"))>=0
                    ?ResourcesCompat.getColor(Utils.getContext().getResources(), R.color.buyColor, null)
                    :ResourcesCompat.getColor(Utils.getContext().getResources(), R.color.sellColor, null));
            viewHolder.tvSellPrice.setText("平仓价  "+json.optString("sellprice"));
            viewHolder.tvSellTime.setText("平仓时间  "+json.optString("selltime"));
        }
        viewHolder.tvTradeItem.setText(json.optString("productName"));
        viewHolder.ivUpOrDown.setImageResource("1".equals(json.optString("buydirection"))?R.mipmap.icon_down:R.mipmap.icon_up);
        viewHolder.tvBuyPrice.setText("建仓价  "+json.optString("buyprice"));
        viewHolder.tvBuyTime.setText("建仓时间  "+json.optString("addtime"));
        viewHolder.tvFee.setText("手续费  "+json.optString("fee"));
        viewHolder.tvTicket.setText("优惠券  "+json.optString("isJuan"));
        viewHolder.tvTopLimit.setText("止盈  "+json.optString("toplimit")+"%");
        viewHolder.tvBottomLimit.setText("止损  "+json.optString("bottomlimit")+"%");
    }

    @Override
    public int getItemCount() {
        return datas.length();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvTradeItem;
        private final ImageView ivUpOrDown;
        private final ImageView ivHandOrAuto;
        private final TextView tvProfitContent;
        private final TextView tvBuyPrice;
        private final TextView tvSellPrice;
        private final TextView tvFee;
        private final TextView tvBottomLimit;
        private final TextView tvBuyTime;
        private final TextView tvSellTime;
        private final TextView tvTicket;
        private final TextView tvTopLimit;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTradeItem = itemView.findViewById(R.id.tv_trade_item);
            ivUpOrDown = itemView.findViewById(R.id.iv_up_or_down);
            ivHandOrAuto = itemView.findViewById(R.id.iv_hand_or_auto);
            tvProfitContent = itemView.findViewById(R.id.tv_profit_content);
            tvBuyPrice = itemView.findViewById(R.id.tv_buy_price);
            tvSellPrice = itemView.findViewById(R.id.tv_sell_price);
            tvFee = itemView.findViewById(R.id.tv_fee);
            tvBottomLimit = itemView.findViewById(R.id.tv_bottom_limit);
            tvBuyTime = itemView.findViewById(R.id.tv_buy_time);
            tvSellTime = itemView.findViewById(R.id.tv_sell_time);
            tvTicket = itemView.findViewById(R.id.tv_ticket);
            tvTopLimit = itemView.findViewById(R.id.tv_top_limit);
        }
    }
}
